package service.impl;

import dao.NoteDao;
import dao.impl.NoteDaoImpl;
import pojo.Note;
import pojo.NoteLabels;
import service.LabelService;
import service.NoteLabelsService;
import service.NoteService;

import java.sql.SQLException;


public class NoteServiceImpl implements NoteService {
    NoteDao noteDao = new NoteDaoImpl();
    private boolean flag = false;

    public boolean addNote(Note note) throws SQLException {
        flag = noteDao.addNote(note);
        return flag;
    }


    public boolean deleteNote(Integer nid) throws SQLException {
        flag = noteDao.delNote(nid);
        return flag;
    }


    public boolean updateNote(Integer nid, Note note) throws SQLException {
        flag = noteDao.updateNote(nid, note);
        return flag;
    }


    public Note findUpdateNote(Integer nid) throws SQLException, ClassNotFoundException {
        Note note = noteDao.findNote(nid);
        return note;
    }


    public boolean passNote(Integer nid) throws SQLException {
        flag = noteDao.updateStyle(nid);
        return flag;
    }


    public Integer findUserNoteNid(Note note) throws SQLException {
        Integer number = 0;
        number = noteDao.findUserNoteNid(note);
        return number;
    }


    public void noteAddLabels(String[] labels, String[] userAddLabels, Note noteadd) throws SQLException {
        LabelService labelService = new LabelServiceImpl();
        NoteService noteService = new NoteServiceImpl();
        NoteLabelsService noteLabelsService = new NoteLabelsServiceImpl();
        for (String label : labels) {
            NoteLabels noteLabels = new NoteLabels();
            Integer lid = labelService.findLabelLid(label);
            Integer nid = noteService.findUserNoteNid(noteadd);
            noteLabels.setNid(nid);
            noteLabels.setLid(lid);
            noteLabelsService.addNoteLabels(noteLabels);
        }
        //自定义标签处理
        if (!userAddLabels[0].equals("无")) {
            boolean isExist = false;
            for (String userAddLabel : userAddLabels) {
                NoteLabels addLabel = new NoteLabels();
                //是否已存在该标签
                isExist = labelService.labelIsExist(userAddLabel);
                //未存在
                if (isExist == false) {
                    labelService.addUserLabel(userAddLabel);
                    //异常处理
                }
                Integer is = 0;
                for (int i = 0; i < labels.length; i++) {
                    is = 0;
                    if (labels[i].equals(userAddLabel)) {
                        is = 1;
                        break;
                    }
                }
                if (is == 0) {
                    Integer lidAdd = labelService.findLabelLid(userAddLabel);
                    Integer nidAdd = noteService.findUserNoteNid(noteadd);
                    addLabel.setLid(lidAdd);
                    addLabel.setNid(nidAdd);
                    noteLabelsService.addNoteLabels(addLabel);
                }

            }
        }
    }
}
